#pragma once

#include <iostream>
#include <string>

// 用 do while包裹后，不管在什么语法结构里调用Die(1),宏展开后都是一个完整的、带有花括号的语句块，不会破坏外层的语法
#define Die(code)   \
	do              \
	{               \
		exit(code); \
	} while (0)
#define CONV(v) (struct sockaddr *)(v) // converse 数据格式转换

enum
{
	USAGE_ERR = 1,
	SOCKET_ERR,
	BIND_ERR,
	LISTEN_ERR
};

// 1.正常字符串  2.out空串&&返回true  3.out空串&&返回false
bool ParseOneLine(std::string &str, std::string *out, std::string sep)
{
	auto pos = str.find(sep);
	if (pos == std::string::npos)
		return false;

	*out = str.substr(0, pos);
	str.erase(0, pos + sep.size());
	return true;
}

// Connection: keep-alive
bool SplitString(const std::string &header, const std::string &sep, std::string *key, std::string *value) // 分割字符串
{
	auto pos = header.find(sep);
	if (pos == std::string::npos)
		return false;
	
	*key = header.substr(0,pos);
	*value = header.substr(pos+sep.size());
	return true;
}
